<template>
  <div class="">
    <!-- 表单 -->
    <el-form ref="form" :model="form" :inline="true" label-width="80px">

        <el-form-item>


        </el-form-item>
      </el-form>


      <el-dialog
        title="添加读者"
        :inline="true"
        center
        :visible.sync="dialogFormVisible"
      >


      <el-form :model="ruleForm">
        <el-form-item label="图书号" size="mini">
          <el-input v-model="ruleForm.card"></el-input>
        </el-form-item>
        <el-form-item label="图书名称" size="mini">
          <el-input v-model="ruleForm.bookname"></el-input>
        </el-form-item>
        <el-form-item label="读者账号" size="mini">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="读者姓名" size="mini">
          <el-input v-model="ruleForm.adminname"></el-input>
        </el-form-item>
        <el-form-item label="借阅日期" size="mini">
          <el-input v-model="ruleForm.begintime"></el-input>
        </el-form-item>
        <el-form-item label="截止还书日期" size="mini">
          <el-input v-model="ruleForm.endtime"></el-input>
      </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser()">确 定</el-button>

      </div>
    </el-dialog>

    <!-- 表格 -->
    <el-table :data="tableData.list" border style="width:100%">

      <el-table-column
        prop="card"
        label="图书号"
        align="center"
        width="150"
      ></el-table-column>
      <el-table-column
        prop="bookname"
        label="图书名称"
        width="150"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="username"
        label="读者账号"
        align="center"
        width="150"
      ></el-table-column>
      <el-table-column
        prop="adminname"
        label="读者姓名"
        width="150"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="begintime"
        label="借阅日期"
        width="150"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="endtime"
        label="截止还书日期"
        width="150"
        align="center"
      ></el-table-column>



      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row.hid)"
            >还书</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.hid)"
            >延期</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div style="margin-top:30px;text-align:center">
      <el-pagination
        background
        layout="prev, pager, next"
        :total="tableData.total"
        :page-size="tableData.pageSize"
        :current-page="tableData.pageNum"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </div>

    <el-dialog
      title="提示"
      :visible.sync="centerDialogVisible"
      width="30%"
      center
    >
      <span>确认还书？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="deleteUser()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  components: {},
  data() {
    return {
      form: {
        name: ""
      },
      ids: -1, //删除的id
      ruleForm: {
        name: "",
        password: "",
        age: "",
        address: "",
        birthday: "",
        phone: ""
      },
      tableData: [],
      currentPage: 1, //当前页
      dialogFormVisible: false, //新增用户弹框
      centerDialogVisible: false //删除弹框



    };
  },
  //vue生命周期函数,页面加载时进行触发
  created() {
    this.finaAll();
  },
  mounted() {},
  watch: {},
  computed: {},
  methods: {
    //模糊查询
    search() {
      this.finaAll();
    },

    handleDelete(id) {
      this.ids = id;
      this.centerDialogVisible = true;
    },

    //点击触发分页
    handleCurrentChange(val) {
      this.currentPage = val;
      this.finaAll();
    },

    deleteUser() {
      console.log("=====================================");
      this.$http
        .get("http://localhost:8090/deleteBook?bid=" + this.ids)
        .then(result => {
          if (result.data.code == 3000) {
            this.$message({
              message: result.data.msg,
              type: "success"
            });
            this.finaAll(); // 刷新页面
          }
        })
        .catch(err => {});
      this.centerDialogVisible = false;
    },

    handleDelete() {
      console.log("=====================================");
      this.$http
        .get("http://localhost:8090/deleteBook?bid=" + this.ids)
        .then(result => {
          if (result.data.code == 3000) {
            this.$message({
              message: result.data.msg,
              type: "success"
            });
            this.finaAll(); // 刷新页面
          }
        })
        .catch(err => {});
      this.centerDialogVisible = false;
    },

    //增加用户
    addBook() {
      //前端怎么传递对象到后端
      let params = new URLSearchParams();
      params.append("name", this.ruleForm.name);
      params.append("password", this.ruleForm.password);
      params.append("age", this.ruleForm.age);
      params.append("address", this.ruleForm.address);
      params.append("birthday", this.ruleForm.birthday);
      params.append("phone", this.ruleForm.phone);

      this.$http
        .post("http://localhost:8090/addBook", params)
        .then(result => {
          if (result.data.code == 2000) {
            this.$message({
              message: result.data.msg,
              type: "success"
            });
            this.finaAll(); // 刷新页面
          }
        })
        .catch(err => {});
      this.dialogFormVisible = false;
    },

    //查询所有的方法
    finaAll() {
      this.$http
        .get("http://localhost:8090/HistoryListOne", {
          params: {
            name: this.form.name,
            pageNum: this.currentPage
          }
        })
        .then(result => {
          console.log(result);
          this.tableData = result.data.data;
        })
        .catch(err => {});
    }
  }
};
</script>
<style scoped></style>
